<?php
## include required files
/*******************************/
require_once '../../includefiles.php';
include_once("../../model/users.php");
require_once '../../model/trade.php';
require_once '../../redirect.php';
## Create Objects
/*******************************/
$userObj = new Model_Users();
$tradeObj = new Model_TRADE();
/*******************************/

checkUserLogin();

if($_SESSION['user_type'] == '3')
{
	@header("location:".SITE_URL."/");
	exit;
}


$userId = $_SESSION['getbids_id'];
$userDetArray = $userObj->getUserDetailsByUserId($userId);
$smarty->assign('userDetArray',$userDetArray);

if(isset($_POST['cat_name'])) 
{
	extract($_POST);
	
	//echo"<pre>";print_R($_POST);die();
	if(trim($_POST['cat_name']) != '') 
	{
		extract($_POST);
		$catArray['trade_category']   = return_post_value($cat_name);
		if($_GET['id3'] == 'edit' && $_GET['id4']!='')
		{
			$catArray['parent_id']   = base64_decode($_GET['id2']);
		}
		else
		{
			$catArray['parent_id']   = 0;
		}
		
		$catArray['user_id']   = $_SESSION['getbids_id'];
		
		if($_GET['id3'] == 'edit' && $_GET['id4']!='')
		{
			$jobtypeid = base64_decode($_GET['id4']);
			$getdetailtradetype = $tradeObj->checkTradeCatNameExits($_POST['cat_name'],$jobtypeid);
			if($getdetailtradetype['id']=='')
			{
				$getdetailjobtype = $tradeObj->checkJobTypeName($_POST['cat_name'],$jobtypeid);
				if($getdetailjobtype['id']=='')
				{
					$tradeObj->editTradeCategoryById($catArray,$jobtypeid);
					$_SESSION['msg'] = "<div class='success_msg'><span>Job Type edited successfully</span></div>";
					redirect_to_link(SITE_URL.'/myaccount/'.$_GET['id1'].'/'.$_GET['id2'].'/mytradecategory/');
						exit;
				}
				else
				{
					$_SESSION['msg'] = "<div class='error_msg'><span>Job type name already used</span></div>";
					
				}

			}
			else
			{
				$_SESSION['msg'] = "<div class='error_msg'><span>Category name already used</span></div>";
				
			}		
		}
		else
		{
			$tradeid = base64_decode($_GET['id1']);
			$getdetailtradetype = $tradeObj->checkTradeCatNameExits($_POST['cat_name'],$tradeid);
			if($getdetailtradetype['id']=='')
			{
				$tradeObj->editTradeCategoryById($catArray,$tradeid);
				$_SESSION['msg'] = "<div class='success_msg'><span>Trade category edited successfully</span></div>";			
				redirect_to_link(SITE_URL.'/myaccount/mytradecategory/');
				exit;
			}
			else
			{
				$_SESSION['msg'] = "<div class='error_msg'><span>Trade category name already used</span></div>";
				
			}	
		}
	
	} 
	else
	{ // php validation else
		$_SESSION['msg'] = '<div class="error_msg"><span>Please enter trade category</span></div>';		
	}
	// main if closed					
}

if($_GET['id1'] != ''&& $_GET['id2'] == 'edit') {
	
	$trade_id = base64_decode($_GET['id1']);
	$tradecatArray = $tradeObj->getTradeCategoryDetailsById($trade_id);
	$smarty->assign('tradecatArray', $tradecatArray);
}
if($_GET['id1'] != ''&& $_GET['id2'] != '' && $_GET['id3'] == 'edit') {

	$trade_id = base64_decode($_GET['id4']);
	$tradecatArray = $tradeObj->getTradeCategoryDetailsById($trade_id);
	$smarty->assign('tradecatArray', $tradecatArray);
	if($_GET['id2']!='')
	{
		$pid= base64_decode($_GET['id2']); 
		$parentArray=$tradeObj->getTradeCategoryDetailsById($pid);
		$smarty->assign('parentArray', $parentArray);
		//echo '<pre>';print_r($moduelArray);exit;
	}

}
if($_SESSION['msg'] != ''){
	$smarty->assign("msg", $_SESSION['msg']);	
	unset($_SESSION['msg']);
}
$active = "myaccount";
$smarty->assign("active", $active);
$subactive ="mytradecategory";
$smarty->assign("subactive", $subactive);
$smarty->assign('mainmenu', '3');
$smarty->display(TEMPLATEDIR . '/modules/myaccount/edittradecategory.tpl');
?>